package org.grapes.wine.api.server.startup;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.grapes.wine.api.server.model.dao.LandscapeDAO;
import org.grapes.wine.api.server.persistence.DAOOperate;

public class InitLandscapeDatabase {
	private static String excelPath = "WebContent/WEB-INF/db/excelDB/Landscape.xls";

	public static void init() {
		try {
			POIFSFileSystem fileSystem = new POIFSFileSystem(
					new FileInputStream(excelPath));
			HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
			HSSFSheet sheet = workbook.getSheetAt(0);

			HSSFRow row = null;
			LandscapeDAO landscapeDAO = new LandscapeDAO();
			int i = 1;
			do {
				row = sheet.getRow(i);
				if (row == null)
					break;

				landscapeDAO.setLandscapeName(row.getCell(0)
						.getRichStringCellValue().getString());
				landscapeDAO.setIntroduction(row.getCell(1)
						.getRichStringCellValue().getString());
				landscapeDAO.setPrice((int) row.getCell(2)
						.getNumericCellValue());
				landscapeDAO.setSightName(row.getCell(3)
						.getRichStringCellValue().getString());
				landscapeDAO.setVisitPeriod((int) row.getCell(4)
						.getNumericCellValue());
				landscapeDAO.setSerial((int) row.getCell(5)
						.getNumericCellValue());
				DAOOperate.create(landscapeDAO);
				i++;
			} while (true);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
